package com.shujia.day15;

public class Code05ThreadCount {
    public static void main(String[] args)  {
        /*
            使用两个线程 对一个变量进行求累加 看最终的累加结果
         */

        CountThread countThread = new CountThread();

        Thread thread1 = new Thread(countThread, "1号");
        Thread thread2 = new Thread(countThread, "2号");


        thread1.start();
        thread2.start();

        try {
            thread1.join();   // 将thread1线程先执行
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            thread2.join(); // 将thread2线程再执行
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}


class CountThread implements Runnable{
    int countNUm = 0;

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            countNUm += 1;
            System.out.println(Thread.currentThread().getName()+"CountNum:"+countNUm);
        }
    }
}
